package com.utilsapi.update_app_store;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;

import java.util.ArrayList;
import java.util.List;

/**
 * @anthor liuwenwang
 * QQ:2653256624
 * @time 2018/1/8 9:42
 * @Describe  手机只有安装对应的应用市场才可以使用
 */
public class Utils {
   public static final String qqDownLaoder_uri="com.tencent.android.qqdownloader";
   public static final String helper_360_uri="com.qihoo.appstore";
   public static final String wandoujia_uri="com.wandoujia.phoenix2";
   public static final String androidMarket_uri="com.hiapk.marketpho";
   public static final String leshi_uri="com.letv.app.appstore";
   public static final String sanxing_uri="com.sec.android.app.samsungapps";
    /**
     * @param context 上下文
     * @param packageName 要升级的应用包名
     */
    //系统有默认一种应用市场时直接跳转到该应用市场，没有的话就会弹出选择应用市场
    public static void goToDefulatMarket(Context context, String packageName) {
        Uri uri = Uri.parse("market://details?id=" + packageName);
        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
        try {
            context.startActivity(goToMarket);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param context 上下文
     * @param packageName 要升级的应用包名
     */
    //跳转到应用宝
    public static void goToQQdownloaderMarket(Context context, String packageName) {
        Uri uri = Uri.parse("market://details?id=" + packageName);
        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
        try {
            goToMarket.setClassName("com.tencent.android.qqdownloader", "com.tencent.pangu.link.LinkProxyActivity");
            context.startActivity(goToMarket);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param context 上下文
     * @param packageName 要升级的应用包名
     */
    //跳转到三星应用市场
    public static void goToSamsungappsMarket(Context context, String packageName) {
        Uri uri = Uri.parse("http://www.samsungapps.com/appquery/appDetail.as?appId=" + packageName);
        Intent goToMarket = new Intent();
        goToMarket.setClassName("com.sec.android.app.samsungapps", "com.sec.android.app.samsungapps.Main");
        goToMarket.setData(uri);
        try {
            context.startActivity(goToMarket);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param context 上下文
     * @param packageName 要升级的应用包名
     */
    //跳转到乐视应用市场
  public  static void goToLeTVStoreDetail(Context context, String packageName) {
        Intent intent = new Intent();
        intent.setClassName("com.letv.app.appstore", "com.letv.app.appstore.appmodule.details.DetailsActivity");
        intent.setAction("com.letv.app.appstore.appdetailactivity");
        intent.putExtra("packageName", packageName);

      try {
          context.startActivity(intent);
      } catch (ActivityNotFoundException e) {
          e.printStackTrace();
      }
    }

    /*
      获取手机中安装的应用市场包名
     */
    public static List<String> getInstalledMarkect(Context context){
        ArrayList packs=new ArrayList();
        Intent intent = new Intent();
        intent.setAction("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.APP_MARKET");
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
        int size = infos.size();
        for (int i = 0; i < size; i++) {
            ActivityInfo activityInfo = infos.get(i).activityInfo;
            String packageName = activityInfo.packageName;
            //获取应用市场的包名
            packs.add(packageName);
        }
      return packs;
    }

    /**
     * 打开指定的应用市场
     * @param context
     * @param packetName  待升级app包名
     * @param marketName  应用市场名称
     */
    public static void openMarket(Context context,String packetName,String marketName){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.parse("market://details?id=" +packetName);//app包名
        intent.setData(uri);
        intent.setPackage(marketName);//应用市场包名
        //intent.setPackage("com.tencent.android.qqdownloader ");//应用市场包名
        try {
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }
}
